// import 'package:flutter/material.dart';
// import 'package:dio/dio.dart';
// // import 'package:http/http.dart' as http; //导入前需要配置

// class HomePage extends StatelessWidget {
//   @override
//   Widget build(BuildContext context) {
//     getHttp();

//     return Scaffold(
//         body: Center(
//       child: Text('商城首页'),
//     ));
//   }

//   void getHttp() async {
//     // var uri = Uri.parse('http://localhost:8080/simple/get?a=1&b=2');
//     // http.Response response = await http.Client().get(uri);

//       try {
//         Response response;
//         // var data = {'name': '技术胖'};
//         response = await Dio().get( "http://localhost:8080/simple/get", );
//         return print(response);
//       } catch (e) {
//         return print(e);
//       }
//     }
// }

import 'package:flutter/material.dart';
import 'package:dio/dio.dart';

class HomePage extends StatefulWidget {
  _HomePageState createState() => _HomePageState();
}


class _HomePageState extends State<HomePage> {
  TextEditingController typeController = TextEditingController();
  String showText = '欢迎你来到美好人间';

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Scaffold(
          appBar: AppBar(
            title: Text('美好人间'),
          ),
          body: SingleChildScrollView(
              child: Container(
            height: 1000,
            child: Column(
              children: <Widget>[
                TextField(
                  controller: typeController,
                  decoration: InputDecoration(
                      contentPadding: EdgeInsets.all(10.0),
                      labelText: '美女类型',
                      helperText: '请输入你喜欢的类型'),
                  autofocus: false,
                ),
                RaisedButton(
                  onPressed: _choiceAction,
                  child: Text('选择完毕'),
                ),
                Text(
                  showText,
                  overflow: TextOverflow.ellipsis,
                  maxLines: 2,
                ),
              ],
            ),
          ))),
    );
  }

  void _choiceAction() {
    print('开始选择你喜欢的类型............');
    if (typeController.text.toString() == '') {
      showDialog(
          context: context,
          builder: (context) => AlertDialog(title: Text('美女类型不能为空')));
    } else {
      getHttp(typeController.text.toString()).then((val) {
        print(val);
        setState(() {
          showText = val.name;
        });
      }).catchError((e) => {print(e)});
    }
  }

  Future getHttp(String typeText) async {
    Dio().get("https://api.apiopen.top/recommendPoetry").then((res) {
      print(res);
      return {'name': typeText};
    }).catchError((e) {
      print(e);
    });
    
    try {
      var response = await Dio().get("https://api.apiopen.top/recommendPoetry");
      print(response);
    } catch (e) {
      print(e);
    }
  }
}
